package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import play.*;
import views.html.pageErreurConnexion;
import views.html.ConsoleAdmin.*;

import models.*;

/**
 * Manage a database of themes
 */
public abstract class AdminTheme extends Controller {

	/**
	 * This result directly redirect to application home.
	 */
	public final static Result GO_HOME = redirect(routes.AdminTheme.listTheme(
			0, "libelle_t", "asc", ""));

	/**
	 * Handle default path requests, redirect to themes list
	 */
	public static Result indexTheme() {
		if (Application.estAdmin()) {
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the paginated list of themes.
	 * 
	 * @param page
	 *            Current page number (starts from 0)
	 * @param sortBy
	 *            Column to be sorted
	 * @param order
	 *            Sort order (either asc or desc)
	 * @param filter
	 *            Filter applied on theme names
	 */
	public static Result listTheme(final int page, final String sortBy,

	final String order, final String filter) {
		if (Application.estAdmin()) {
			return ok(listTheme.render(
					Theme.page(page, 10, sortBy, order, filter), sortBy, order,
					filter));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'edit form' of a existing Theme.
	 * 
	 * @param id
	 *            Id of the theme to edit
	 */
	public static Result editTheme(final Long id) {
		if (Application.estAdmin()) {
			final Form<Theme> themeForm = form(Theme.class).fill(
					Theme.findById(id));
			return ok(editTheme.render(id, themeForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'edit form' submission
	 * 
	 * @param id
	 *            Id of the theme to edit
	 */
	public static Result updateTheme(final Long id) {
		if (Application.estAdmin()) {
			final Form<Theme> themeForm = form(Theme.class).bindFromRequest();
			if (themeForm.hasErrors()) {
				return badRequest(editTheme.render(id, themeForm));
			}

			themeForm.get().update(id);
			flash("success", "Theme " + themeForm.get().getLibelleT()
					+ " has been updated");
			Logger.debug("Mise à jour theme : " + themeForm.get().getIdTheme()
					+ themeForm.get().getLibelleT() + " par user "
					+ session().get("id"));
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'new theme form'.
	 */
	public static Result createTheme() {
		if (Application.estAdmin()) {
			final Form<Theme> themeForm = form(Theme.class);
			return ok(createTheme.render(themeForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'new theme form' submission
	 */
	public static Result saveTheme() {
		if (Application.estAdmin()) {
			final Form<Theme> themeForm = form(Theme.class).bindFromRequest();
			if (themeForm.hasErrors()) {
				return badRequest(createTheme.render(themeForm));
			}
			themeForm.get().save();
			flash("success", "Theme " + themeForm.get().getLibelleT()
					+ " has been created");
			Logger.debug("Création theme : " + themeForm.get().getIdTheme()
					+ themeForm.get().getLibelleT() + " par user "
					+ session().get("id"));
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle theme deletion
	 */
	public static Result deleteTheme(final Long id) {
		if (Application.estAdmin()) {
			final Theme t = Theme.findById(id);
			flash("success", "Theme has been deleted");
			Logger.debug("Suppression theme : " + t.getIdTheme()
					+ t.getLibelleT() + " par user " + session().get("id"));
			Theme.delete(id);
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

}
